<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>portal</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>
<body>
<div class="layui-form layuimini-form">
    
    <div class="layui-form-item">
        <label class="layui-form-label required">菜单标题</label>
        <div class="layui-input-block">
            <input type="text" name="title" lay-verify="required" lay-reqtext="菜单标题不能为空" placeholder="请输入菜单标题" value="" class="layui-input">
            <tip>填写标题。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label ">菜单链接</label>
        <div class="layui-input-block">
            <input type="text" name="href" placeholder="请输入菜单页面链接" value="" class="layui-input">
            <tip>推荐使用相对portal目录的路径，例如：page/upload.html</tip>
        </div>
    </div>
			
        <div class="layui-form-item">
        <label class="layui-form-label ">父菜单ID</label>
        <div class="layui-input-block">
        	<div  id="form_view_menu_id_ipt">
            
            </div>
            <tip>父菜单ID，为0时将显示在顶部菜单位置。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label ">当前菜单ID</label>
        <div class="layui-input-block">
        	<div  id="form_view_menu_id_ipt2">
            
            </div>
            <tip>推荐项目手动使用10001~99999之间的ID，未填写由时系统自动随机生成。</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label ">菜单图标</label>
        <div class="layui-input-block">
            <input type="text" name="icon" placeholder="请输入菜单图标" value="fa fa-rocket" class="layui-input">
            <tip id="layerTip" ><button class="layui-btn layui-btn-xs layui-btn-normal" data-method="viewAllIcon" class="layui-btn">查看全部图标</button></tip>
        </div>
    </div>
    <div class="layui-form-item">
        <label class="layui-form-label ">排序值</label>
        <div class="layui-input-block">
            <input type="text" name="sort_num" placeholder="请输入排序值，整数" value="0" class="layui-input">
            <tip>值越小越在前面</tip>
        </div>
    </div>
    
    <div class="layui-form-item">
        <label class="layui-form-label ">窗口</label>
        <div class="layui-input-block">
            <input type="radio" name="target" value="_self" title="当前窗口打开" checked="">
            <input type="radio" name="target" value="_blank" title="新窗口打开">
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit lay-filter="saveBtn">添加新菜单</button>
        </div>
    </div>
</div>

        
    <script id="form_tpl_id" type="text/html">
	<input type="text" name="parent_id" id="parent_id_ipt"  placeholder="父菜单ID" value="{{d.pid}}" class="layui-input">
	</script>
	<script id="form_tpl_id2" type="text/html">
    <input type="text" name="id" id="id_ipt" placeholder="当前菜单ID" value="{{d.rand}}" class="layui-input">
	</script>
	
<script src="../../lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use(['laytpl', 'form', 'layer'], function () {
        var form = layui.form,
            layer = layui.layer,
            laytpl = layui.laytpl,
            $ = layui.$;

        //触发事件
        var active = {
        	viewAllIcon: function () {
        		layer.open({
        			  type: 2, 
        			  title: '全部图标',
        			  area: ['800px', '400px'],
        			  content: '/portal/page/icon.html' //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        		}); 
        	}
        }
        
        $('#layerTip .layui-btn').on('click', function(){
            var othis = $(this), method = othis.data('method');
            active[method] ? active[method].call(this, othis) : '';
          });
        
        var pid = sessionStorage.getItem('add_parent_id'); // router.search.id
        var getTpl = form_tpl_id.innerHTML,
            view = document.getElementById('form_view_menu_id_ipt');
        laytpl(getTpl).render({pid: pid || 0,}, function(html){
              view.innerHTML = html;
        });

        var x = 99999;
        var y = 10001;
        var rand = parseInt(Math.random() * (x - y + 1) + y);
        var getTpl2 = form_tpl_id2.innerHTML,
            view2 = document.getElementById('form_view_menu_id_ipt2');
        laytpl(getTpl2).render({rand: rand}, function(html){
              view2.innerHTML = html;
        });
        
        //监听提交
        form.on('submit(saveBtn)', function (data) {

            $.ajax({
                url: '/?s=Portal.Page.AddNewMenu',
                type:'post',
                data: data.field,
                success:function(data){
                    if (data.ret != 200) {
                        layer.msg(data.msg);
                        return;
                    }

                    var index = layer.alert('新菜单添加成功，请刷新', function () {
                        // 关闭弹出层
                        layer.close(index);

                        var iframeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(iframeIndex);
                    });
                },
                complete: function () {
                }
            });
            return false;
        });

    });
</script>
</body>
</html>
